//
//  GUIAlertView.m
//  DreamOf
//
//  Created by 薛小刚 on 2018/3/29.
//  Copyright © 2018年 薛小刚. All rights reserved.
//

#import "GUIAlertView.h"
#import "GUILabel.h"

@interface GUIAlertView()

@property (nonatomic, strong) UIView * alertView;

@property (nonatomic, strong) GUILabel * titleLabel;

@property (nonatomic, strong) UIImageView * loadingImageView;




@end

@implementation GUIAlertView
static GUIAlertView *manager;

+ (GUIAlertView *)defaultAlertView {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[GUIAlertView alloc] init];
    });
    return manager;
}



- (UILabel *)titleLabel {
    if (nil == _titleLabel) {
        _titleLabel = [[GUILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
        _titleLabel.backgroundColor = [UIColor blackColor];
        _titleLabel.layer.masksToBounds = YES;
        _titleLabel.layer.cornerRadius = 3;
        _titleLabel.font = [UIFont systemFontOfSize:getLittleFont()];
        _titleLabel.textColor = [UIColor whiteColor];
    }
    return _titleLabel;
}

- (UIView *)alertView {
    if (nil == _alertView) {
        _alertView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 110, 120)];
        _alertView.backgroundColor = [UIColor blackColor];
        _alertView.layer.masksToBounds = YES;
        _alertView.layer.cornerRadius = 10;
        _alertView.center = CGPointMake([UIScreen mainScreen].bounds.size.width / 2., [UIScreen mainScreen].bounds.size.height / 2.);
    }
    return _alertView;
}

- (UIImageView *)loadingImageView {
    if (nil == _loadingImageView) {
        _loadingImageView = [[UIImageView alloc] initWithFrame:CGRectMake(manager.alertView.bounds.size.width / 2. - 25, manager.alertView.bounds.size.height / 2. - 40, 50, 50)];
        _loadingImageView.image = [UIImage imageNamed:@"loading"];
        [[GUIAlertView defaultAlertView].alertView addSubview:_loadingImageView];
        _loadingImageView.contentMode = UIViewContentModeCenter;
    }
    return _loadingImageView;
}

+ (void)showTitle:(NSString *)title {
    [GUIAlertView defaultAlertView].titleLabel.text = [NSString stringWithFormat:@"%@", title];
    manager.titleLabel.contentInset = UIEdgeInsetsMake(8, 12, 8, 12);
    [manager.titleLabel sizeToFit];
    manager.titleLabel.center = CGPointMake([UIScreen mainScreen].bounds.size.width / 2., [UIScreen mainScreen].bounds.size.height / 2.);
    [manager addSubview:manager.titleLabel];
    [[UIApplication sharedApplication].keyWindow addSubview:manager];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [manager.titleLabel removeFromSuperview];
        [manager removeFromSuperview];
    });
}

+ (void)showAlertTitle:(NSString *)title description:(NSString *)description leftButtonTitle:(NSString *)leftButton rightButtonTitle:(NSString *)rightButton context:(UIViewController *) context withBlock:(void(^)(BOOL isLeftButton)) block {
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:title message:description preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction * leftAction = [UIAlertAction actionWithTitle:leftButton style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        block(YES);
    }];
    UIAlertAction * rightAction = [UIAlertAction actionWithTitle:rightButton style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        block(NO);
    }];
    [alertController addAction:leftAction];
    [alertController addAction:rightAction];
    if (context == nil) {
        [[BaseTabBar defaultBaseTabBar].tabBarController presentViewController:alertController animated:YES completion:nil];
    } else {
        [context presentViewController:alertController animated:YES completion:nil];
    }
}
+ (void)showAlertTitle:(NSString *)title description:(NSString *)description buttonTitle:(NSString *)buttonTitle context:(UIViewController *) context withBlock:(void(^)(void)) block {
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:title message:description preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction * leftAction = [UIAlertAction actionWithTitle:buttonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        block();
    }];
    
    [alertController addAction:leftAction];
    if (context == nil) {
        [[BaseTabBar defaultBaseTabBar].tabBarController presentViewController:alertController animated:YES completion:nil];
    } else {
        [context presentViewController:alertController animated:YES completion:nil];
    }
}
+ (void)loadingWithTitle:(NSString *)title {
    [[UIApplication sharedApplication].keyWindow addSubview:[GUIAlertView defaultAlertView]];
    [manager addSubview:manager.alertView];
    [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionRepeat | UIViewAnimationOptionCurveLinear animations:^{
        manager.loadingImageView.transform = CGAffineTransformMakeRotation(M_PI);
    } completion:^(BOOL finished) {

    }];
}

+ (void)hidden {
    [[GUIAlertView defaultAlertView].alertView removeFromSuperview];
    [manager removeFromSuperview];
}
@end
